//
//  LWZExclusiveViewController.m
//  MusicFM
//
//  Created by 王景欣 on 16/1/14.
//  Copyright © 2016年 LWZ. All rights reserved.
//

#import "LWZExclusiveViewController.h"
#define  ImageHeight 250.f
#define NAVBAR_CHANGE_POINT 10

@interface LWZExclusiveViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, copy) NSString *stringUrl;

@property (nonatomic, strong) UIImageView *imageView;

@property (nonatomic, strong) UIImageView *backImageView;

@property (nonatomic, strong) NSMutableArray *dataSourceArray;

@property (nonatomic, strong) UIVisualEffectView *visualEffect;

@property (nonatomic, copy) NSString *titleImageLabelString;

@property (nonatomic, copy) NSString *imageUrl;

@end

@implementation LWZExclusiveViewController

- (instancetype)initWithStringURL:(NSString *)stringURL WithTitleImageLabelString:(NSString *)string withImageUrl:(NSString *)imageUrl
{
    self = [super init];
    if (self) {
        self.stringUrl = stringURL;
        self.imageUrl = imageUrl;
        self.titleImageLabelString = string;
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    
//    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0 , 100, 44)];
//    titleLabel.backgroundColor = [UIColor clearColor];  //设置Label背景透明
//    titleLabel.textColor = [UIColor whiteColor];  //设置文本颜色
//    titleLabel.textAlignment = NSTextAlignmentCenter;
//    titleLabel.text = @"Demo1";  //设置标题
//    self.navigationItem.titleView = titleLabel;
    

//    [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor clearColor]];

    self.backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
    self.backImageView.image = [UIImage imageNamed:@"backImage"];
    [self.view addSubview:self.backImageView];
    
    [self createTableView];
    [self createdImageView];
    [self handleData];
}
#pragma mark - 创建ImageView
- (void)createdImageView {
    
    self.imageView = [[UIImageView alloc] init];
    self.imageView.frame = CGRectMake(0, -ImageHeight, kScreenWidth, ImageHeight);
    [self.imageView yy_setImageWithURL:[NSURL URLWithString:self.imageUrl] options:YYWebImageOptionProgressiveBlur];
    self.imageView.contentMode = 2;
    self.imageView.clipsToBounds = YES;
    
    self.visualEffect = [[UIVisualEffectView alloc] initWithEffect:UIBlurEffectStyleExtraLight];
    self.visualEffect.frame = CGRectMake(0, 0, kScreenWidth, 400);
    self.visualEffect.alpha = 0.7;
    self.visualEffect.backgroundColor = [UIColor grayColor];
    [self.imageView addSubview:self.visualEffect];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    titleLabel.center = self.visualEffect.center;
    titleLabel.backgroundColor = [UIColor clearColor];  //设置Label背景透明
    titleLabel.textColor = [UIColor magentaColor];  //设置文本颜色
    titleLabel.textAlignment = 1;
    titleLabel.text = self.titleImageLabelString;  //设置标题
    [self.visualEffect addSubview:titleLabel];
    
    [self.tableView addSubview:self.imageView];
}
#pragma mark - 滑动方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    NSLog(@"%f", scrollView.contentOffset.y);

    if (scrollView.contentOffset.y < -ImageHeight) {
        CGRect frame = self.imageView.frame;
        frame.origin.y = scrollView.contentOffset.y;
        frame.size.height = - scrollView.contentOffset.y;
        self.imageView.frame = frame;
    }
    UIColor * color = [UIColor colorWithRed:0.941 green:0.284 blue:0.364 alpha:1.000];
    if (scrollView.contentOffset.y > NAVBAR_CHANGE_POINT) {
        CGFloat alpha = MIN(1, 1 - ((NAVBAR_CHANGE_POINT + 64 - scrollView.contentOffset.y) / 64));
        self.navigationController.navigationBar.hidden = NO;
//        self.navigationController.navigationBar.tintColor = [UIColor redColor];
        [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:alpha]];
        
    } else {
        self.navigationController.navigationBar.hidden = YES;
        [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:0]];
    }
}


#pragma mark - 创建TableView
- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 49) style:UITableViewStyleGrouped];
    self.tableView.separatorColor = [UIColor colorWithRed:0.73 green:0.81 blue:0.96 alpha:1];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.contentInset = UIEdgeInsetsMake(ImageHeight, 0, 0, 0);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCellIdentifier"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"LWZHostPlayTableViewCell" bundle:nil] forCellReuseIdentifier:@"LWZHostPlayTableViewCellIdentifier"];
    
    [self.view addSubview:self.tableView];
}
#pragma mark - 处理数据
- (void)handleData {
    
    NSString *string = kFMHostExURL;
    self.stringUrl = [NSString stringWithFormat:string, self.stringUrl];
    
    [MHNetWorkTask getWithURL:self.stringUrl withParameter:nil withHttpHeader:nil withResponseType:ResponseTypeJSON withSuccess:^(id result) {

        NSArray *array = [NSArray arrayWithArray:[result valueForKey:@"list"]];
        NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dic in array) {
            LWZHostPlayModel *hostPlayModel = [[LWZHostPlayModel alloc] initWithDataSource:dic];
            [mutableArray addObject:hostPlayModel];
        }
        self.dataSourceArray = [NSMutableArray arrayWithArray:mutableArray];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    } withFail:^(NSError *error) {
        
    }];
}
#pragma mark - TableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSourceArray.count;//
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LWZHostPlayTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LWZHostPlayTableViewCellIdentifier"];
    cell.playModel = self.dataSourceArray[indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    return cell;

    
    
}
#pragma mark - HEIGHT
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}
#pragma mark - TableView点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    LWZHostPlayModel *model = self.dataSourceArray[indexPath.row];
    LWZFMPlayViewController *playVC = [LWZFMPlayViewController sharePlayerViewController];
    playVC.song_id = model.trackId;
//    [LWZFMPlayViewController sharePlayerViewController];
    playVC.indexRow = indexPath.row;
    [self presentViewController:playVC animated:YES completion:nil];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
